/******************************************************************************
* SVN­Version­Information:
* ­­­­­­­­­­­­­­­­­­­­­­­­---------------------
* $LastChangedRevision: 6 $
* $LastChangedBy: c.a.duevel $
* $LastChangedDate: 2009-08-26 15:12:37 +0000 (Wed, 26 Aug 2009) $
* $HeadURL: http://robofight.googlecode.com/svn/trunk/ClientJavaFX/src/de/hft_stuttgart/robofight/client/javafx/RobotNode.fx $
******************************************************************************/

package de.hft_stuttgart.robofight.client.javafx;

import de.hft_stuttgart.robofight.client.javafx.Arena;
import javafx.animation.Interpolator;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.scene.Cursor;
import javafx.scene.CustomNode;
import javafx.scene.effect.DropShadow;
import javafx.scene.Group;
import javafx.scene.input.MouseEvent;
import javafx.scene.Node;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Line;

/**
 * @author ralf.dauenhauer@gmx.de
 */

// Node Class for Robots
public class RobotNode extends CustomNode{

    package var image;
    package var robotColorId : Integer;
    package var teamId : Integer;
    package var xCoord : Number;
    package var yCoord : Number;
    var rocketX : Number;
    var rocketY : Number;
    var angle : Number;
    package var energy : java.lang.Double = 25;
    package var arena : Arena;
    package var robotId : Integer;
    package var setActive : function(robotId : Integer);
    package var isActive : Boolean = false;
    var isShooting : Boolean = false;
    public var deadBot : Node;
    var isAlive : Boolean = true on replace {
        arena.playSoundByName("botDied");
        deadBot.visible = true;
    };

    public var weaponBullet : Node;
    public var chassis : Node;
    public var shield : Node;
    public var weapon : Node;
    public var weaponSound : String;

    package function getRobotId(): Integer {
        return this.robotId;
    }

   var robotFrame = Circle {
                layoutX : 33, layoutY: 25
                radius: 46
                fill: null
                stroke : Color.GREENYELLOW
                strokeWidth: 4
                strokeDashOffset : 0
                strokeDashArray : [12,7];
                opacity : 0.9;
                visible : bind isActive;
    }

    var energyBar = Group {
        layoutX : 20, layoutY: -20
        visible: bind isAlive
        content : [
             Line {
                startX: 2, startY: 2
                endX: 25, endY: 2
                strokeWidth: 4
                stroke: Color.BLACK
            }
              Line {
                startX: 2, startY: 2
                endX: bind this.energy, endY: 2
                strokeWidth: 2
                stroke: Color.LIGHTGREEN
            }
        ]
    }

    var robotGroup : Group = Group {
            rotate : bind angle;
            cursor: Cursor.HAND
            visible: bind isAlive
            content: [
                robotFrame,
                    Group {
                    content : chassis
                }
                shield,
                weapon,
            ]
            onMouseClicked : function( e: MouseEvent ):Void {
                setActive(this.robotId as Integer);
                isActive = true;
            }
    };

    // rotateTranslation, rotates the robot to direction of the next position
    function rotateRobot(x: Integer, y: Integer){
        angle = -java.lang.Math.atan2(x - xCoord ,  y -yCoord) *180/java.lang.Math.PI as Number;
    }


    // layoutTransition, moves given node to a new x, y position
    package function moveRobotTo(x : Number, y: Number){
       rotateRobot(x, y);
       Timeline {
       repeatCount: 1
        keyFrames : [
            KeyFrame {
                time: 1000ms
                values: [
                xCoord => x tween Interpolator.LINEAR,
                yCoord => y tween Interpolator.LINEAR
                ]
            }
        ]
        }.play();
    }

    var rocket = Group {
        content : weaponBullet;
        layoutX : bind rocketX +28 , layoutY: bind rocketY +28
        visible: false
    }

    public function setEnergy(newEnergyValue : java.lang.Double){
        this.energy = (newEnergyValue as java.lang.Double) / 4;
        if(energy <= 0){
            isAlive = false;
            rocket.visible = false;
        }
    }

    public function shootToRobot(targetId : Integer){       
       arena.playSoundByName(weaponSound);
       Timeline {
       repeatCount: 1
        keyFrames : [
            KeyFrame {
                time: 0ms
                values : [
                rocketX => this.xCoord,
                rocketY => this.yCoord 
                ]
                action : function(){
                    rocket.visible = true;
                }
            }
            KeyFrame {
                time: 500ms
                values: [
                rocketX => (arena.robotNodes.get(targetId) as RobotNode).xCoord  tween Interpolator.LINEAR,
                rocketY => (arena.robotNodes.get(targetId) as RobotNode).yCoord  tween Interpolator.LINEAR
                ]
                action : function(){
                    rocket.visible = false;
                }
            }
        ]
        }.play();
    }

     var robotNode = Group {
        layoutX: bind xCoord, layoutY: bind yCoord
        content: [
            robotGroup,
            energyBar,
            Group {
                    content: deadBot
                    visible: bind not isAlive
            }
        ]

        effect: DropShadow {
                    offsetX: 0
                    offsetY: 0
                    color: Constants.colors[robotColorId]
                    radius: 30
                    spread: 0.4
      }
    }

    override function create() : Node {
       insert rocket into arena.group.content;
       return robotNode
    }

}
